using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data;
using System.Data.Linq;
using System.IO;
using System.Linq;
using System.Linq.Dynamic;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Caching;
using System.Web.Script.Services;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Xml;
using Data;
using Data.DataSources;
using Data.LINQ;
using Dimok.BlackBook;
using Dimok.Data;
using Dimok.Data.Database;
using Dimok.Exceptions;
using Dimok.Web.UI;

namespace WSA{
	[ScriptService]
	[WebService(Namespace = "http://motobankcorp/manager/Admin/")]
	[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
	public class Admin : Dimok.Aspx.Manager.Lib.WS {

		#region User
		[WebMethod]
		public void SavePagePosition(string pageName, int top, int left, int width, int height) {
			ProfilerAjax.SavePagePosition(pageName, top, left, width, height);
		}
		[WebMethod]
		public string UserPath() {
			string nameSpace = SQL.ExecuteScalar("SELECT dbo.UserNameSpace()") + "";
			return (nameSpace == "" ? "" : "[" + nameSpace + "]") + SQL.ExecuteScalarArray("SELECT GroupName FROM UserGroups() WHERE IsGroup>0 UNION ALL SELECT dbo.UserName()").Join("/");
		}
		#endregion

		#region Locker
		[WebMethod]
		public void UnLockLoan() {
			try {
				Dimok.Locker.LockApplicant(0, 0);
			} catch (Exception exc) { throw new ClientException(exc); }
		}
		[WebMethod]
		public string GetLockInfo() {
			try {
				Dimok.LocksDS ds = Dimok.Locker.LockedDataSet(true, "User='" + uox.UserName + "'");
				return ds.GetXml();
			} catch (Exception exc) { throw new ClientException(exc); }
		}
		#endregion

		#region StateFlow
		[WebMethod]
		public string SF_Tree(string tableName, string principal_id) {
			try {
				//throw new Exception("No table["+tableName+"] name provided.");
				DataView dvSFTree = SQL.StateFlow.StateFlowTree(tableName, principal_id);
				System.Text.StringBuilder sb = new System.Text.StringBuilder();
				List<string> sl = new List<string>(dvSFTree.Count);
				foreach (DataRowView row in dvSFTree)
					sb.Append(row[0]);
				string xml = sb.ToString();
				//System.IO.File.WriteAllText("c:\\temp\\TreeXml.xml", xml);
				return xml;
			} catch (Exception exc) {
				throw new ClientException(exc,"TableName",tableName);
			}
		}
		private string SF_Tree_Old(string tableName, int principal_id) {
			try {
				//throw new Exception("No table["+tableName+"] name provided.");
				DataView dvSFTree = SQL.StateFlow.StateFlowTree(tableName, principal_id);
				List<string> sl = new List<string>(dvSFTree.Count);
				foreach (DataRowView row in dvSFTree)
					sl.Add(row[0] + "");
				return string.Join(Environment.NewLine, sl.ToArray());
			} catch (Exception exc) {
				throw new Exception("TableName:" + tableName + Environment.NewLine + ClientException.GetExceptions(exc), exc);
			}
		}
		[WebMethod]
		public void SF_Add(int stateFromID, int stateToID, string principal_id) {
			SQL.StateFlow.StateFlowAddPath(stateFromID, stateToID, principal_id);
		}
		[WebMethod]
		public void SF_Rem(int stateFromID, int stateToID) {
			SQL.StateFlow.StateFlowRemPath(stateFromID, stateToID);
		}
		[WebMethod]
		public void SFG_Add(int stateFromID, int stateToID, string principal_id) {
			SQL.StateFlow.StateFlowAddGroup(stateFromID, stateToID, principal_id);
		}
		[WebMethod]
		public void SFG_Rem(int stateFromID, int stateToID, string principal_id) {
			SQL.StateFlow.StateFlowRemGroup(stateFromID, stateToID, principal_id);
		}
		#endregion
	}

}
